/*
 * net_aseq.c
 *
 *  Created on: 2023年8月16日
 *      Author: mcealt_DS
 *              HXF
 */

#include "net_aseq.h"
#include "device.h"
#include "string.h"
#include "app_log.h"
#include "device_nvm.h"
#include "bsp_nvm.h"

#define ASEQ_DEFAULT 0
#define ECHASEQ_DEFAULT 0

#ifdef HUB_DEVICE
static uint8_t nodeAseq[ASEQ_NUM][NODE_MAX_COUNT]; //hub用来保存各设备的aseq
#endif
#ifdef RFM_DEVICE
static uint8_t Aseq[ASEQ_NUM]; //rfm用来保存收到（回显）的aseq
#endif

void init_aseq(void)
{
#ifdef HUB_DEVICE
  int j = 0;
  int i = 0;
  for(i = 0; i < ASEQ_NUM; i++){
      for(j = 0; j < NODE_MAX_COUNT; j++){
          nodeAseq[i][j] = ASEQ_DEFAULT;
      }
  }
#endif
#ifdef RFM_DEVICE
  /*
#if defined(SM_RL) || defined(SM_WS) || defined(SM_SMART_PLUG)
  int i = 0;
  for(i = 0; i < ASEQ_NUM; i++){
      Aseq[i] = ECHASEQ_DEFAULT;
  }
#else
  if(DS_nvmRead(ENbyKeyASeqLed, &Aseq[ASEQ_LED], 1) != NVM_OK){
      Aseq[ASEQ_LED] = ECHASEQ_DEFAULT;
  }
  if(DS_nvmRead(ENbyKeyASeqOperate, &Aseq[ASEQ_OPERATE], 1) != NVM_OK){
      Aseq[ASEQ_OPERATE] = ECHASEQ_DEFAULT;
  }
#endif
*/
  int i = 0;
  for(i = 0; i < ASEQ_NUM; i++){
      Aseq[i] = ECHASEQ_DEFAULT;
  }
#endif
}

void set_aseq(ASEQ_ENUM idx, uint8_t aseq)
{
  if(idx >= ASEQ_NUM)
    return;
#ifdef HUB_DEVICE
  (void)aseq;
#endif
#ifdef RFM_DEVICE
  if((aseq) && (Aseq[idx] != aseq)){
      Aseq[idx] = aseq;
/*
#if defined(SM_RL) || defined(SM_WS) || defined(SM_SMART_PLUG)

#else
      if(idx == ASEQ_LED){
          DS_nvmWrite(ENbyKeyASeqLed, &Aseq[ASEQ_LED], 1);
      }else if(idx == ASEQ_OPERATE){
          DS_nvmWrite(ENbyKeyASeqOperate, &Aseq[ASEQ_OPERATE], 1);
      }
#endif
*/
  }
#ifdef CONFIG_LOG_NET_ASEQ
  DS_app_log_debug("set %s aseq %d\r\n",(idx == ASEQ_LED)?"ledctrl":"operatectrl",aseq);
#endif
#endif
}

uint8_t get_aseq(ASEQ_ENUM idx)
{
  if(idx >= ASEQ_NUM)
    return 0x00;
#ifdef RFM_DEVICE
#ifdef CONFIG_LOG_NET_ASEQ
  DS_app_log_debug("get %s aseq %d\r\n",(idx == ASEQ_LED)?"ledctrl":"operatectrl",Aseq[idx]);
#endif
  return Aseq[idx];
#endif
  return 0;
}

void updata_node_aseq(ASEQ_ENUM idx, uint8_t device_id)
{
  if(idx >= ASEQ_NUM)
    return;
#ifdef HUB_DEVICE
  /*
  if (255 == nodeAseq[idx][device_id])
    nodeAseq[idx][device_id] = 2;
  else
    nodeAseq[idx][device_id]++;
    */
  if(255 == nodeAseq[idx][device_id]){
      nodeAseq[idx][device_id] = 2;
  }else{
      nodeAseq[idx][device_id]++;
  }
#ifdef CONFIG_LOG_NET_ASEQ
    DS_app_log_debug("upa %s node aseq %d\r\n",(idx == ASEQ_LED)?"ledctrl":"operatectrl", nodeAseq[idx][device_id]);
#endif
#endif
#ifdef RFM_DEVICE
    (void)device_id;
#endif
}

uint8_t get_node_aseq(ASEQ_ENUM idx, uint8_t device_id)
{
  if(idx >= ASEQ_NUM)
    return 0;
#ifdef HUB_DEVICE
#ifdef CONFIG_LOG_NET_ASEQ
    DS_app_log_debug("%s node aseq %d\r\n", (idx == ASEQ_LED)?"ledctrl":"operatectrl",nodeAseq[idx][device_id]);
#endif
    return nodeAseq[idx][device_id];
#endif
#ifdef RFM_DEVICE
    (void)device_id;
    return 0;
#endif
}
/*
void set_node_aseq(ASEQ_ENUM idx, uint8_t device_id, uint8_t aseq)
{
  if(idx >= ASEQ_NUM)
    return;
#ifdef HUB_DEVICE
  nodeAseq[idx][device_id] = aseq;
#ifdef CONFIG_LOG_NET_ASEQ
    DS_app_log_debug("set %s node aseq %d\r\n",(idx == ASEQ_LED)?"ledctrl":"operatectrl",nodeAseq[idx][device_id]);
#endif
#endif
#ifdef RFM_DEVICE
    (void)device_id;
    (void)aseq;
#endif
}
*/
bool check_aseq(ASEQ_ENUM idx, uint8_t device_id, uint8_t echASeq)
{
  if(idx >= ASEQ_NUM)
    return false;

#ifdef HUB_DEVICE
#ifdef CONFIG_LOG_NET_ASEQ
  DS_app_log_debug("check %s node aseq %d echASeq %d\r\n",(idx == ASEQ_LED)?"ledctrl":"operatectrl",nodeAseq[idx][device_id],echASeq);
#endif
    return ((nodeAseq[idx][device_id] == echASeq)?true:false);
#endif
#ifdef RFM_DEVICE
    (void)device_id;
#ifdef CONFIG_LOG_NET_ASEQ
    DS_app_log_debug("check %s my aseq %d hub aseq %d\r\n",(idx == ASEQ_LED)?"ledctrl":"operatectrl",Aseq[idx],echASeq);
#endif
    return ((Aseq[idx] == echASeq)?true:false);
#endif
}
